/* 
 * File:   main.cpp
 * Author: Potential
 *
 * Created on August 31, 2013, 5:02 PM
 */

#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <windows.h>
#include <ctime>
#include "Player.h"
#include "Monster.h"

using namespace std;

Player player;
Monster wildMonster;

void encounter() {
    cout << "===============================" << endl;
    cout << "= Encountered a wild monster! =" << endl;
    cout << "===============================" << endl;
}

void playerAttackPrompt() {
    string pause;
    cout << "Press A and then enter to attack...";
    cin >> pause;
    cout << endl;

    int playerDamage = 0;

    playerDamage = player.getMonster()->getDamage() - wildMonster.getArmor();

    cout << player.getMonster()->getName() << " attacks " << wildMonster.getName() << " for " << playerDamage
            << " damage." << endl << endl;

    wildMonster.setCurHealth(wildMonster.getCurHealth() - playerDamage);

    Sleep(2000);
}

void enemyAttack() {
    int enemyDamage = 0;

    enemyDamage = wildMonster.getDamage() - player.getMonster()->getArmor();
    cout << wildMonster.getName() << " attacks " << player.getMonster()->getName() << " for " << enemyDamage
            << " damage." << endl << endl;

    player.getMonster()->setCurHealth(player.getMonster()->getCurHealth() - enemyDamage);

    Sleep(2000);
}

void enemyDefeat() {
    cout << wildMonster.getName() << " has been defeated." << endl << endl;
    cout << player.getMonster()->getName() << " has gained " <<
            wildMonster.getMaxExperience() << " points.\n\n";
}

void battleWildMonster() {
    int randomMonster = 1 + (rand() % 6);
    wildMonster.monsterSelection(randomMonster);
    bool playerDefeated = false;
    bool wildMonsterDefeated = false;
    encounter();
    
    Sleep(2000);

    while (playerDefeated == false && wildMonsterDefeated == false) {
        player.displayStats();
        wildMonster.displayStats();
        playerAttackPrompt();

        if (wildMonster.getCurHealth() > 0) {
            player.displayStats();
            wildMonster.displayStats();
            enemyAttack();
        } else {
            wildMonsterDefeated = true;
            enemyDefeat();
            player.getMonster()->gainExperience(wildMonster.getMaxExperience());
        }

        if (player.getMonster()->getCurHealth() <= 0) {
            playerDefeated = true;
        }
    }

    if (playerDefeated == false) {
        //Victory banner
        cout << "===== VICTORY!!! =====" << endl << endl;
    }

    if (playerDefeated == true) {
        //Defeat banner
        cout << "===== DEFEAT!!! =====" << endl << endl;
    }

    player.getMonster()->resetHealth();
    wildMonster.resetHealth();
}

int gameMenu(){
    int input;
    cout << "Game Menu:\n";
    cout << "1. Battle wild monster\n";
    cout << "2. View Stats\n";
    cout << "3. Quit\n";
    cout << "What would you like to do? ";
    cin >> input;
    return input;
}

int main(int argc, char** argv) {
    srand(static_cast<unsigned int> (time(0))); // seed random number

    bool quit = false;
    int option = 0;
    string input = "";

    //Welcome banner
    cout << "Welcome to Virtual Monsters!\n";
    cout << "Please enter a name for your character.\n";
    cout << "Character Name: ";
    cin >> input;
    player.setName(input);
    
    cout << "\nPlease select your pet monster.\n";
    cout << "1. Dragon\n";
    cout << "2. Phoenix\n";
    cout << "3. Leviathan\n";
    cout << "4. Golem\n";
    cout << "5. Pegasus\n";
    cout << "6. Cerberus\n";
    cout << "Monster selection: ";
    cin >> option;
    player.setMonster(option);
    player.getMonster()->levelUp(); //start at level 1
    player.getMonster()->resetHealth();
    
    cout << "\nWould you like to name your pet? (1=Yes, 0=No): ";
    cin >> option;
    if (option) {
        cout << "\nPlease enter a name for your monster.\n";
        cout << "Monster Name: ";
        cin >> input;
        player.getMonster()->setName(input);
    }
    
    cout << "\nWelcome " << player.getName() << "!\n\n";
    
    Sleep(2000);
    system("cmd.exe /c cls");

    while (!quit) {
        option = gameMenu();
        if (option == 1) {
            battleWildMonster();
        } else if (option == 2) {
            player.displayStats();
        } else {
            quit = true;
        }
    }

    return 0;
}